#ifndef __CORE_EXEC_H__
#define __CORE_EXEC_H__

/**
 *  core/exec.h
 *  spousteni aplikaci
 */

#include <core/filesys/fat12.h>

/* "magic" fail return value */
#define EXEC_FAILED   (0x7FF8)

/* pevne stanovene adresy */
#define EXEC_DIR_PTR        (SYSADDR + 0x1000)
#define EXEC_ARG_PTR        (SYSADDR + 0x1100)
#define EXEC_APPNAME_PTR    (SYSADDR + 0x1200)
#define EXEC_RETURN_VALUE   (SYSADDR + 0x1300)

/* stavy jednotlivych segmentu */
bool _exec_segment_used[16] = {
    true,  true,  false, false, false, false, false, false,
    true /* 0x8000 < ENV data */, false, false, false, false, true,  true,  true
};

/* tabulka segmentu */
static long _exec_far_pointer_table[16] = {
    0x00000000, 0x10000000, 0x20000000, 0x30000000, 0x40000000,
    0x50000000, 0x60000000, 0x70000000, 0x80000000, 0x90000000,
    0xA0000000, 0xB0000000, 0xC0000000, 0xD0000000, 0xE0000000,
    0xF0000000
};

/* makro, ktere provede "call far far_seg:0000" */
#define exec_call_far(far_seg) \
          asm db 0x9a                       ; \
          asm dw 0000                       ; \
          asm dw far_seg                    ;

unsigned int  exec_first_free_segment       (void);
void          exec_set_segment_status       (unsigned int segment, bool status);
void          exec_load_app                 (unsigned int segment, unsigned int cluster, unsigned int filesize);
int           exec                          (char far *filename);

#include <source/core/exec.c>

#endif
